<?php
// Common global functions
function getPhpDate($inputdate) {
    $startdateArray = explode ( "-", $inputdate );
    $start_month = $startdateArray [1];
    $start_day = $startdateArray [2];
    $start_year = $startdateArray [0];
    return mktime ( 0, 0, 0, $start_month, $start_day, $start_year );
}
//convert 03/08/2006 to 2006-03-08
function getStringDate($inputdate) {
    if (! empty ( $inputdate )) {
        $startdateArray = explode ( "/", $inputdate );
        $start_month = $startdateArray [0];
        $start_day = $startdateArray [1];
        $start_year = $startdateArray [2];
        $start = $start_year . '-' . $start_month . '-' . $start_day;
        return $start;
    }else{
        return $inputdate;
    }
}
//convert  2006-03-08 to 03/08/2006
function getSlashDate($inputdate) {
    $temp = explode ( " ", $inputdate );
    //2006-03-18 or 2006-03-18 00:00:00
    $inputdateArray = explode ( "-", $temp [0] );
    return $inputdateArray [1] . "/" . $inputdateArray [2] . "/" . $inputdateArray [0];
}
//get 2006-3-18 from datetime
function getDateFromTime($inputdatetime) {
    $year = date ( "Y", $inputdatetime );
    $month = date ( "n", $inputdatetime );
    if ($month < 10)
        $month = "0" . $month;
    $day = date ( "j", $inputdatetime );
    if ($day < 10)
        $day = "0" . $day;
    $convertedDate = $year . '-' . $month . '-' . $day;
    return $convertedDate;
}
function resolveDocumentRoot() {
    $current_script = dirname ( $_SERVER ['SCRIPT_NAME'] );
    $current_path = dirname ( $_SERVER ['SCRIPT_FILENAME'] );
    /* work out how many folders we are away from document_root
       by working out how many folders deep we are from the url.
       this isn't fool proof */
    $adjust = explode ( "/", $current_script );
    $adjust = count ( $adjust ) - 1;
    /* move up the path with ../ */
    $traverse = str_repeat ( "../", $adjust );
    $adjusted_path = sprintf ( "%s/%s", $current_path, $traverse );
    /* real path expands the ../'s to the correct folder names */
    return realpath ( $adjusted_path );
}
// grab a random word from dictionary between the two lengths
// and return it
function getRandomNumer($min_length, $max_length) {
    // generate a random word
    $word = '';
    // remember to change this path to suit your system
    $dictionary = '/usr/dict/words'; // the ispell dictionary
    $fp = @fopen ( $dictionary, 'r' );
    if (! $fp)
        return false;
    $size = filesize ( $dictionary );
    // go to a random location in dictionary
    srand ( ( double ) microtime () * 1000000 );
    $rand_location = rand ( 0, $size );
    fseek ( $fp, $rand_location );
    // get the next whole word of the right length in the file
    while ( strlen ( $word ) < $min_length || strlen ( $word ) > $max_length || strstr ( $word, "'" ) ) {
        if (feof ( $fp ))
            fseek ( $fp, 0 ); // if at end, go to start
        $word = fgets ( $fp, 80 ); // skip first word as it could be partial
        $word = fgets ( $fp, 80 ); // the potential password
    }
    ;
    $word = trim ( $word ); // trim the trailing \n from fgets
    return $word;
}

//=====================================================================
// above functions was defined by Gordon
//=====================================================================
/**
 * Pick a language, any language ...
 */
function selectLanguage() {
    $default = 'en';
    $cookie_domain = ($_SERVER ['HTTP_HOST'] != 'localhost') ? $_SERVER ['HTTP_HOST'] : false;
    if (isset ( $_POST ['lang'] )) { //if user changed language
        $langName = $_POST ['lang'];
        setcookie ( 'lang', $langName, time () + 60 * 60 * 24 * 365, '/', $cookie_domain, false );
    } else {
        $langName = isset ( $_COOKIE ['lang'] ) ? $_COOKIE ['lang'] : $default;
    }
    return $langName;
}

/**
 * Generates an alphanumeric random string of given length
 */
function gen_rand_string($num_chars = 8) {
    $rand_str = unique_id ();
    $rand_str = str_replace ( '0', 'Z', strtoupper ( base_convert ( $rand_str, 16, 35 ) ) );
    return substr ( $rand_str, 0, $num_chars );
}
/**
 * Return unique id
 * @param string $extra additional entropy
 */
function unique_id($extra = 'c') {
    static $dss_seeded = false;
    global $config;
    $val = $config ['rand_seed'] . microtime ();
    $val = md5 ( $val );
    $config ['rand_seed'] = md5 ( $config ['rand_seed'] . $val . $extra );
    if ($dss_seeded !== true && ($config ['rand_seed_last_update'] < time () - rand ( 1, 10 ))) {
        set_config ( 'rand_seed', $config ['rand_seed'], true );
        set_config ( 'rand_seed_last_update', time (), true );
        $dss_seeded = true;
    }
    return substr ( $val, 4, 16 );
}
/**
 * Determine whether we are approaching the maximum execution time. Should be called once
 * at the beginning of the script in which it's used.
 * @return	bool	Either true if the maximum execution time is nearly reached, or false
 *					if some time is still left.
 */
function still_on_time($extra_time = 15) {
    static $max_execution_time, $start_time;
    $time = explode ( ' ', microtime () );
    $current_time = $time [0] + $time [1];
    if (empty ( $max_execution_time )) {
        $max_execution_time = (function_exists ( 'ini_get' )) ? ( int ) @ini_get ( 'max_execution_time' ) : ( int ) @get_cfg_var ( 'max_execution_time' );
        // If zero, then set to something higher to not let the user catch the ten seconds barrier.
        if ($max_execution_time === 0) {
            $max_execution_time = 50 + $extra_time;
        }
        $max_execution_time = min ( max ( 10, ($max_execution_time - $extra_time) ), 50 );
        // For debugging purposes
        // $max_execution_time = 10;
        global $starttime;
        $start_time = (empty ( $starttime )) ? $current_time : $starttime;
    }
    return (ceil ( $current_time - $start_time ) < $max_execution_time) ? true : false;
}
/**
 * Checks if a path ($path) is absolute or relative
 *
 * @param string $path Path to check absoluteness of
 * @return boolean
 */
function is_absolute($path) {
    return ($path [0] == '/' || (DIRECTORY_SEPARATOR == '\\' && preg_match ( '#^[a-z]:/#i', $path ))) ? true : false;
}
/**
 * @author Chris Smith <chris@project-minerva.org>
 * @copyright
 * @param string $path The path which we should attempt to resolve.
 * @return mixed
 */
function phpbb_own_realpath($path) {
    // Now to perform funky shizzle
    // Switch to use UNIX slashes
    $path = str_replace ( DIRECTORY_SEPARATOR, '/', $path );
    $path_prefix = '';
    // Determine what sort of path we have
    if (is_absolute ( $path )) {
        $absolute = true;
        if ($path [0] == '/') {
            // Absolute path, *NIX style
            $path_prefix = '';
        } else {
            // Absolute path, Windows style
            // Remove the drive letter and colon
            $path_prefix = $path [0] . ':';
            $path = substr ( $path, 2 );
        }
    } else {
        // Relative Path
        // Prepend the current working directory
        if (function_exists ( 'getcwd' )) {
            // This is the best method, hopefully it is enabled!
            $path = str_replace ( DIRECTORY_SEPARATOR, '/', getcwd () ) . '/' . $path;
            $absolute = true;
            if (preg_match ( '#^[a-z]:#i', $path )) {
                $path_prefix = $path [0] . ':';
                $path = substr ( $path, 2 );
            } else {
                $path_prefix = '';
            }
        } else 
            if (isset ( $_SERVER ['SCRIPT_FILENAME'] ) && ! empty ( $_SERVER ['SCRIPT_FILENAME'] )) {
                // Warning: If chdir() has been used this will lie!
                // Warning: This has some problems sometime (CLI can create them easily)
                $path = str_replace ( DIRECTORY_SEPARATOR, '/', dirname ( $_SERVER ['SCRIPT_FILENAME'] ) ) . '/' . $path;
                $absolute = true;
                $path_prefix = '';
            } else {
                // We have no way of getting the absolute path, just run on using relative ones.
                $absolute = false;
                $path_prefix = '.';
            }
    }
    // Remove any repeated slashes
    $path = preg_replace ( '#/{2,}#', '/', $path );
    // Remove the slashes from the start and end of the path
    $path = trim ( $path, '/' );
    // Break the string into little bits for us to nibble on
    $bits = explode ( '/', $path );
    // Remove any . in the path, renumber array for the loop below
    $bits = array_values ( array_diff ( $bits, array ('.' ) ) );
    // Lets get looping, run over and resolve any .. (up directory)
    for($i = 0, $max = sizeof ( $bits ); $i < $max; $i ++) {
        // @todo Optimise
        if ($bits [$i] == '..') {
            if (isset ( $bits [$i - 1] )) {
                if ($bits [$i - 1] != '..') {
                    // We found a .. and we are able to traverse upwards, lets do it!
                    unset ( $bits [$i] );
                    unset ( $bits [$i - 1] );
                    $i -= 2;
                    $max -= 2;
                    $bits = array_values ( $bits );
                }
            } else 
                if ($absolute) { // ie. !isset($bits[$i - 1]) && $absolute
                    // We have an absolute path trying to descend above the root of the filesystem
                    // ... Error!
                    return false;
                }
        }
    }
    // Prepend the path prefix
    array_unshift ( $bits, $path_prefix );
    $resolved = '';
    $max = sizeof ( $bits ) - 1;
    // Check if we are able to resolve symlinks, Windows cannot.
    $symlink_resolve = (function_exists ( 'readlink' )) ? true : false;
    foreach ( $bits as $i => $bit ) {
        if (@is_dir ( "$resolved/$bit" ) || ($i == $max && @is_file ( "$resolved/$bit" ))) {
            // Path Exists
            if ($symlink_resolve && is_link ( "$resolved/$bit" ) && ($link = readlink ( "$resolved/$bit" ))) {
                // Resolved a symlink.
                $resolved = $link . (($i == $max) ? '' : '/');
                continue;
            }
        } else {
            // Something doesn't exist here!
        // This is correct realpath() behaviour but sadly open_basedir and safe_mode make this problematic
        // return false;
        }
        $resolved .= $bit . (($i == $max) ? '' : '/');
    }
    // @todo If the file exists fine and open_basedir only has one path we should be able to prepend it
    // because we must be inside that basedir, the question is where...
    // @internal The slash in is_dir() gets around an open_basedir restriction
    if (! @file_exists ( $resolved ) || (! is_dir ( $resolved . '/' ) && ! is_file ( $resolved ))) {
        return false;
    }
    // Put the slashes back to the native operating systems slashes
    $resolved = str_replace ( '/', DIRECTORY_SEPARATOR, $resolved );
    // Check for DIRECTORY_SEPARATOR at the end (and remove it!)
    if (substr ( $resolved, - 1 ) == DIRECTORY_SEPARATOR) {
        return substr ( $resolved, 0, - 1 );
    }
    return $resolved; // We got here, in the end!
}
if (! function_exists ( 'realpath' )) {
    /**
     * A wrapper for realpath
     * @ignore
     */
    function phpbb_realpath($path) {
        return phpbb_own_realpath ( $path );
    }
} else {
    /**
     * A wrapper for realpath
     */
    function phpbb_realpath($path) {
        $realpath = realpath ( $path );
        // Strangely there are provider not disabling realpath but returning strange values. :o
        // We at least try to cope with them.
        if ($realpath === $path || $realpath === false) {
            return phpbb_own_realpath ( $path );
        }
        // Check for DIRECTORY_SEPARATOR at the end (and remove it!)
        if (substr ( $realpath, - 1 ) == DIRECTORY_SEPARATOR) {
            $realpath = substr ( $realpath, 0, - 1 );
        }
        return $realpath;
    }
}
if (! function_exists ( 'htmlspecialchars_decode' )) {
    /**
     * A wrapper for htmlspecialchars_decode
     * @ignore
     */
    function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
        return strtr ( $string, array_flip ( get_html_translation_table ( HTML_SPECIALCHARS, $quote_style ) ) );
    }
}
// functions used for building option fields
/**
 * Pick a template/theme combo,
 */
function style_select($default = '', $all = false) {
}
/**
 * Pick a timezone
 */
function tz_select($default = '', $truncate = false) {
    return;
}
//Form validation
/**
 * Add a secret hash   for use in links/GET requests
 * @param string  $link_name The name of the link; has to match the name used in check_link_hash, otherwise no restrictions apply
 * @return string the hash

 */
function generate_link_hash($link_name) {
    global $user;
    if (! isset ( $user->data ["hash_$link_name"] )) {
        $user->data ["hash_$link_name"] = substr ( sha1 ( $user->data ['user_form_salt'] . $link_name ), 0, 8 );
    }
    return $user->data ["hash_$link_name"];
}
/**
 * checks a link hash - for GET requests
 * @param string $token the submitted token
 * @param string $link_name The name of the link
 * @return boolean true if all is fine
 */
function check_link_hash($token, $link_name) {
    return $token === generate_link_hash ( $link_name );
}
// Little helpers
/**
 * Little helper for the build_hidden_fields function
 */
function _build_hidden_fields($key, $value, $specialchar, $stripslashes) {
    $hidden_fields = '';
    if (! is_array ( $value )) {
        $value = ($stripslashes) ? stripslashes ( $value ) : $value;
        $value = ($specialchar) ? htmlspecialchars ( $value, ENT_COMPAT, 'UTF-8' ) : $value;
        $hidden_fields .= '<input type="hidden" name="' . $key . '" value="' . $value . '" />' . "\n";
    } else {
        foreach ( $value as $_key => $_value ) {
            $_key = ($stripslashes) ? stripslashes ( $_key ) : $_key;
            $_key = ($specialchar) ? htmlspecialchars ( $_key, ENT_COMPAT, 'UTF-8' ) : $_key;
            $hidden_fields .= _build_hidden_fields ( $key . '[' . $_key . ']', $_value, $specialchar, $stripslashes );
        }
    }
    return $hidden_fields;
}
/**
 * Build simple hidden fields from array
 *
 * @param array $field_ary an array of values to build the hidden field from
 * @param bool $specialchar if true, keys and values get specialchared
 * @param bool $stripslashes if true, keys and values get stripslashed
 *
 * @return string the hidden fields
 */
function build_hidden_fields($field_ary, $specialchar = false, $stripslashes = false) {
    $s_hidden_fields = '';
    foreach ( $field_ary as $name => $vars ) {
        $name = ($stripslashes) ? stripslashes ( $name ) : $name;
        $name = ($specialchar) ? htmlspecialchars ( $name, ENT_COMPAT, 'UTF-8' ) : $name;
        $s_hidden_fields .= _build_hidden_fields ( $name, $vars, $specialchar, $stripslashes );
    }
    return $s_hidden_fields;
}
/**
 * Parse cfg file
 */
function parse_cfg_file($filename, $lines = false) {
    $parsed_items = array ();
    if ($lines === false) {
        $lines = file ( $filename );
    }
    foreach ( $lines as $line ) {
        $line = trim ( $line );
        if (! $line || $line [0] == '#' || ($delim_pos = strpos ( $line, '=' )) === false) {
            continue;
        }
        // Determine first occurrence, since in values the equal sign is allowed
        $key = strtolower ( trim ( substr ( $line, 0, $delim_pos ) ) );
        $value = trim ( substr ( $line, $delim_pos + 1 ) );
        if (in_array ( $value, array ('off', 'false', '0' ) )) {
            $value = false;
        } else 
            if (in_array ( $value, array ('on', 'true', '1' ) )) {
                $value = true;
            } else 
                if (! trim ( $value )) {
                    $value = '';
                } else 
                    if (($value [0] == "'" && $value [sizeof ( $value ) - 1] == "'") || ($value [0] == '"' && $value [sizeof ( $value ) - 1] == '"')) {
                        $value = substr ( $value, 1, sizeof ( $value ) - 2 );
                    }
        $parsed_items [$key] = $value;
    }
    return $parsed_items;
}
/**
 * This function returns a regular expression pattern for commonly used expressions
 * Use with / as delimiter for email mode and # for url modes
 * mode can be: email|bbcode_htm|url|url_inline|www_url|www_url_inline|relative_url|relative_url_inline|ipv4|ipv6
 */
function get_preg_expression($mode) {
    switch ($mode) {
        case 'email' :
            return '(?:[a-z0-9\'\.\-_\+\|]++|&amp;)+@[a-z0-9\-]+\.(?:[a-z0-9\-]+\.)*[a-z]+';
            break;
        case 'bbcode_htm' :
            return array (
                    '#<!\-\- e \-\-><a href="mailto:(.*?)">.*?</a><!\-\- e \-\->#', 
                    '#<!\-\- l \-\-><a (?:class="[\w-]+" )?href="(.*?)(?:(&amp;|\?)sid=[0-9a-f]{32})?">.*?</a><!\-\- l \-\->#', 
                    '#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="(.*?)">.*?</a><!\-\- \1 \-\->#', 
                    '#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#', 
                    '#<!\-\- .*? \-\->#s', 
                    '#<.*?>#s' );
            break;
        // Whoa these look impressive!
        // The code to generate the following two regular expressions which match valid IPv4/IPv6 addresses
        // can be found in the develop directory
        case 'ipv4' :
            return '#^(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$#';
            break;
        case 'ipv6' :
            return '#^(?:(?:(?:[\dA-F]{1,4}:){6}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:::(?:[\dA-F]{1,4}:){5}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:):(?:[\dA-F]{1,4}:){4}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,2}:(?:[\dA-F]{1,4}:){3}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,3}:(?:[\dA-F]{1,4}:){2}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,4}:(?:[\dA-F]{1,4}:)(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,5}:(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,6}:[\dA-F]{1,4})|(?:(?:[\dA-F]{1,4}:){1,7}:))$#i';
            break;
        case 'url' :
        case 'url_inline' :
            $inline = ($mode == 'url') ? ')' : '';
            $scheme = ($mode == 'url') ? '[a-z\d+\-.]' : '[a-z\d+]'; // avoid automatic parsing of "word" in "last word.http://..."
            // generated with regex generation file in the develop folder
            return "[a-z]$scheme*:/{2}(?:(?:[a-z0-9\-._~!$&'($inline*+,;=:@|]+|%[\dA-F]{2})+|[0-9.]+|\[[a-z0-9.]+:[a-z0-9.]+:[a-z0-9.:]+\])(?::\d*)?(?:/(?:[a-z0-9\-._~!$&'($inline*+,;=:@|]+|%[\dA-F]{2})*)*(?:\?(?:[a-z0-9\-._~!$&'($inline*+,;=:@/?|]+|%[\dA-F]{2})*)?(?:\#(?:[a-z0-9\-._~!$&'($inline*+,;=:@/?|]+|%[\dA-F]{2})*)?";
            break;
        case 'www_url' :
        case 'www_url_inline' :
            $inline = ($mode == 'www_url') ? ')' : '';
            return "www\.(?:[a-z0-9\-._~!$&'($inline*+,;=:@|]+|%[\dA-F]{2})+(?::\d*)?(?:/(?:[a-z0-9\-._~!$&'($inline*+,;=:@|]+|%[\dA-F]{2})*)*(?:\?(?:[a-z0-9\-._~!$&'($inline*+,;=:@/?|]+|%[\dA-F]{2})*)?(?:\#(?:[a-z0-9\-._~!$&'($inline*+,;=:@/?|]+|%[\dA-F]{2})*)?";
            break;
        case 'relative_url' :
        case 'relative_url_inline' :
            $inline = ($mode == 'relative_url') ? ')' : '';
            return "(?:[a-z0-9\-._~!$&'($inline*+,;=:@|]+|%[\dA-F]{2})*(?:/(?:[a-z0-9\-._~!$&'($inline*+,;=:@|]+|%[\dA-F]{2})*)*(?:\?(?:[a-z0-9\-._~!$&'($inline*+,;=:@/?|]+|%[\dA-F]{2})*)?(?:\#(?:[a-z0-9\-._~!$&'($inline*+,;=:@/?|]+|%[\dA-F]{2})*)?";
            break;
    }
    return '';
}
/**
 * Returns the first block of the specified IPv6 address and as many additional
 * ones as specified in the length paramater.
 * If length is zero, then an empty string is returned.
 * If length is greater than 3 the complete IP will be returned
 */
function short_ipv6($ip, $length) {
    if ($length < 1) {
        return '';
    }
    // extend IPv6 addresses
    $blocks = substr_count ( $ip, ':' ) + 1;
    if ($blocks < 9) {
        $ip = str_replace ( '::', ':' . str_repeat ( '0000:', 9 - $blocks ), $ip );
    }
    if ($ip [0] == ':') {
        $ip = '0000' . $ip;
    }
    if ($length < 4) {
        $ip = implode ( ':', array_slice ( explode ( ':', $ip ), 0, 1 + $length ) );
    }
    return $ip;
}
/**
 * Generate page header
 */
function page_header() {
    global $config, $lang, $template;
    if (defined ( 'HEADER_INC' )) {
        return;
    }
    define ( 'HEADER_INC', true );
    // application/xhtml+xml not used because of IE
    header ( 'Content-type: text/html; charset=UTF-8' );
    header ( 'Cache-Control: private, no-cache="set-cookie"' );
    header ( 'Expires: 0' );
    header ( 'Pragma: no-cache' );
    // The following assigns all _common_ variables that may be used at any point in a template.
    $template->assign ( array ('contentEncoding' => 'UTF-8', 
            'siteName' => $config ['siteName'], 
            'themePath' => $template->template_dir, 
            'label' => array ('email' => $lang ['EMAIL'], 
                    'password' => $lang ['PASSWORD'], 
                    'signIn' => $lang ['SIGN_IN'], 
                    'register' => $lang ['REGISTER'], 
                    'forgetPassword' => $lang ['FORGET_PASSWORD'], 
                    'msgRequired' => $lang ['MSG_REQUIRED'], 
                    'msgMinChars' => $lang ['MSG_MIN_CHARS'], 
                    'msgEmailFormat' => $lang ['MSG_EMAIL_FORMAT'] ), 
            'link' => array ('register' => 'index.php?action=register', 
                    'forgetPassword' => 'index.php?action=forgetPw', 
                    'root' => REGE_ROOT . 'index.php' ) ) );
    return;
}

/**
 * Error and message handler, call with trigger_error if required
 */
function errorHandler($errno, $msg_text, $errfile, $errline) {
    if (defined ( 'REGE_DEBUG' ) && REGE_DEBUG) {
        if (defined ( "REGE_AJAX_CALL" )) {
            $response = array ("success" => false, 
                    'errorMessage' => $msg_text );
            echo json_encode ( $response );
        } else {
            // with the risk of changing backslashes meant to be there
            $errfile = str_replace ( array (
                    phpbb_realpath ( REGE_ROOT ), 
                    '\\' ), array ('', 
                    '/' ), $errfile );
            $msg_text = str_replace ( array (
                    phpbb_realpath ( REGE_ROOT ), 
                    '\\' ), array ('', 
                    '/' ), $msg_text );
            $requestUri = @$_SERVER ['REQUEST_URI'];
            switch ($errno) {
                case E_ERROR :
                    $errType = "Error";
                    break;
                case E_WARNING :
                    $errType = "Warning";
                    break;
                case E_PARSE :
                    $errType = "Parse Error";
                    break;
                case E_NOTICE :
                    $errType = "Notice";
                    break;
                case E_CORE_ERROR :
                    $errType = "Core Error";
                    break;
                case E_CORE_WARNING :
                    $errType = "Core Warning";
                    break;
                case E_COMPILE_ERROR :
                    $errType = "Compile Error";
                    break;
                case E_COMPILE_WARNING :
                    $errType = "Compile Warning";
                    break;
                case E_USER_ERROR :
                    $errType = "User Error";
                    break;
                case E_USER_WARNING :
                    $errType = "User Warning";
                    break;
                case E_USER_NOTICE :
                    $errType = "User Notice";
                    break;
                case E_STRICT :
                    $errType = "Strict Notice";
                    break;
                case E_RECOVERABLE_ERROR :
                    $errType = "Recoverable Error";
                    break;
                default :
                    $errType = "Unknown error ($errno)";
                    break;
            }
            ?>

<table align="center" border="1" cellspacing="0"
	style="background: white; color: black; width: 80%;">
	<tr>
		<th colspan=2 style="background: #DA9832; text-align: center; font-weight: bold;">REGe Debug Error</th>
	</tr>
	<tr>
		<td style="font-weight: bold; background: #BFECFA; vertical-align: top;">Message:</td>
		<td><?php
            echo $msg_text;
            ?></td>
	</tr>
	<tr>
		<td style="font-weight: bold; background: #BFECFA; vertical-align: top;">File:</td>
		<td><?php
            echo $errfile;
            ?></td>
	</tr>
	<tr>
		<td style="font-weight: bold; background: #BFECFA; vertical-align: top;" nowrap>Line:</td>
		<td><?php
            echo $errline;
            ?></td>
	</tr>
	<tr>
		<td style="font-weight: bold; background: #BFECFA; vertical-align: top;" nowrap>Errno:</td>
		<td><?php
            echo $errno;
            ?></td>
	</tr>

	<tr>
		<td style="font-weight: bold; background: #BFECFA; vertical-align: top;" nowrap>ErrType:</td>
		<td><?php
            echo $errType;
            ?></td>
	</tr>

	<tr>
		<td style="font-weight: bold; background: #BFECFA; vertical-align: top;">Script:</td>
		<td><a href="<?php
            echo $requestUri;
            ?>"><?php
            echo $requestUri;
            ?></a></td>
	</tr>
</table>
<?php
        }
    } // end of if 
//    exit ();
} // end of error handler


function exceptionHandler($exception) {
    echo "Uncaught exception: ", $exception->getMessage (), "\n";
}

?>
